package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.CardDetail;

import java.util.List;

/**
 * @author Denis
 * @ClassName: CardAdapter
 * @Description: 卡显示适配器
 * @date 2017/6/7 15:24
 */

public class CardAdapter extends DelegateAdapter.Adapter<CardAdapter.PeopleHolder> {

    private Context context;
    private LayoutHelper helper;
    private List<CardDetail> datas;
    private OnChangeClickListener onChangeClickListener;
    private OnXiaoClickListener onXiaoClickListener;

    public CardAdapter(Context context, LayoutHelper helper, List<CardDetail> datas) {
        this.context = context;
        this.helper = helper;
        this.datas = datas;
    }

    public void setOnChangeClickListener(OnChangeClickListener onChangeClickListener) {
        this.onChangeClickListener = onChangeClickListener;
    }

    public void setOnXiaoClickListener(OnXiaoClickListener onXiaoClickListener) {
        this.onXiaoClickListener = onXiaoClickListener;
    }

    public void setDatas(List<CardDetail> datas) {
        this.datas = datas;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return helper;
    }


    @Override
    public PeopleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_card, parent, false);
        return new PeopleHolder(view);
    }

    @Override
    public void onBindViewHolder(PeopleHolder holder, int position) {
        final CardDetail cardDetail = datas.get(position);
        if(cardDetail.card_type!=null && cardDetail.card_type.equals("0")){
            holder.tvName.setText("身份证");
        }else if(cardDetail.card_type!=null && cardDetail.card_type.equals("1")){
            holder.tvName.setText("IC卡");
        }else{
            holder.tvName.setText("未知类型");
        }
        holder.tvCardNum.setText("卡号：" + cardDetail.card_id);
        holder.tv_card_time.setText("有效期：" + cardDetail.card_end_time);
        holder.ivIcon.setImageResource(R.mipmap.icon_out);
        holder.ivIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.show(context, "减少");
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChangeClickListener != null) {
                    onChangeClickListener.onChangeClickListener(cardDetail);
                }
                if (onXiaoClickListener != null) {
                    onXiaoClickListener.onXiaoClickListener(cardDetail);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    protected class PeopleHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public TextView tvCardNum, tv_card_time;
        public ImageView ivIcon;

        public PeopleHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvCardNum = (TextView) itemView.findViewById(R.id.tv_card_num);
            ivIcon = (ImageView) itemView.findViewById(R.id.iv_icon);
            tv_card_time = (TextView) itemView.findViewById(R.id.tv_card_time);
        }
    }

    public interface OnChangeClickListener {
        void onChangeClickListener(CardDetail cardDetail);
    }

    public interface OnXiaoClickListener {
        void onXiaoClickListener(CardDetail cardDetail);
    }
}
